<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="角色名称" prop="role">
            <Input v-model="formValidate.role" placeholder="输入角色名称"></Input>
        </FormItem>
        <FormItem label="角色编码" prop="code">
            <Input v-model="formValidate.code" placeholder="输入角色编码"></Input>
        </FormItem>
        <FormItem label="描述" prop="description">
            <Input v-model="formValidate.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入角色描述"></Input>
        </FormItem>
        <FormItem >
            <Button   @click="handleCancel()" style=" float: right; ">取消</Button>
            <Button  type="primary" @click="handlePost('formValidate')" style=" float: right; margin-right: 8px" >保存</Button>
        </FormItem>
    </Form>
</template>
<script>
    import api  from '../../../libs/api';
    import {eventBus} from "../../../libs/eventBus"
    export default {
        name: 'role_add',
        data() {
            return {
                formValidate: {
                    role: '',
                    code: '',
                    description: ''
                },
                ruleValidate: {
                    role: [
                        { required: true, message: '角色名称不允许为空', trigger: 'blur' }
                    ],
                    code: [
                        { required: true, message: '角色编码不允许为空', trigger: 'blur' }
                    ],
                    description: [
                        { required: true, message: '请输入角色描述', trigger: 'blur' },
                        { type: 'string', min: 4, message: '角色描述长度不小于4', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            handlePost (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$post(api.roles.base,this.formValidate).then(response => {
                            if(200==response.code){
                                this.$refs[name].resetFields();
                                eventBus.$emit("closeModal",false);
                                eventBus.$emit("freshenRoleList");
                            }
                        });
                    }
                })
            },
            handleCancel(){
                this.$refs["formValidate"].resetFields();
                eventBus.$emit("closeModal",false);
            },
        }
    }
</script>